#include <gtk/gtk.h>
#include <stdio.h>

#include "gtkutils.h"
#include "system.h"

void sayHello() {
    printf("hello word\n");
}

#define createObjType(obj, type) type *obj;
typedef void(*createLabelObj);

createObjType(model, GtkLabel);
createObjType(type, GtkLabel);
createObjType(hardversion, GtkLabel);
createObjType(sn, GtkLabel);

createObjType(firmware, GtkLabel);
createObjType(kernel, GtkLabel);
createObjType(architecture, GtkLabel);

createObjType(runtime, GtkLabel);
createObjType(systime, GtkLabel);
createObjType(load, GtkLabel);

createObjType(cnt, GtkLabel);

guint timer;
long count = 0;

createTimerFunc(update_info) {
    System s = {0};
    bool ret = initSystem(&s);

    gtk_label_set_text(model, s.cmdline.Model);
    gtk_label_set_text(hardversion, s.cmdline.Hardversion);
    gtk_label_set_text(type, s.cmdline.Type);
    gtk_label_set_text(sn, s.cmdline.SN);

    gtk_label_set_text(firmware, s.Firmware_Version);
    gtk_label_set_text(kernel, s.Kernel_Version);
    gtk_label_set_text(architecture, s.Architecture);

    gtk_label_set_text(runtime, s.Runtime);
    gtk_label_set_text(systime, s.Systime);
    gtk_label_set_text(load, s.Load);

    char buffer[100] ={0};
    sprintf(buffer, "查询次数：%ld", count++);
    gtk_label_set_text(cnt, buffer);
    

    return true;
}

createSignalFunc(timer_start) {
    if(!timerStarted(&timer))
        timer = createTimerMilliSecond(20, update_info, NULL);
}
createSignalFunc(timer_stop) {
    stopTimer(&timer);
}

static void activate(GtkApplication *app, gpointer user_data) {
    GtkWindow *window;

    model = createLabel("(null)");
    type = createLabel("(null)");
    hardversion = createLabel("(null)");
    sn = createLabel("(null)");
    firmware = createLabel("(null)");
    kernel = createLabel("(null)");
    architecture = createLabel("(null)");
    runtime = createLabel("(null)");
    systime = createLabel("(null)");
    load = createLabel("(null)");
    cnt = createLabel("查询次数：0");

    window = createWindowWithTitleSize(app, "gtk-timeout-system-info-test", 200, 100);
    
    GtkBox *timer_ctl_box = createBoxWithHorizntalAppends(5, 
        createButtonAction(NULL, "Start",timer_start),
        createButtonAction(NULL, "Stop",timer_stop),
        cnt,
        NULL
    );

    setBoxHorizntalAlign(timer_ctl_box, ACENTER);

    GtkBox *mainbox = createBoxWithVerticalAppends(5, 
        createBoxWithHorizntalAppends(5, createLabel("Model: "),             model ,        NULL),
        createBoxWithHorizntalAppends(5, createLabel("Type: "),              type ,         NULL),
        createBoxWithHorizntalAppends(5, createLabel("Hardversion: "),       hardversion ,  NULL),
        createBoxWithHorizntalAppends(5, createLabel("SN: "),                sn ,           NULL),
        createSeparatorWithHorizntal(),
        createBoxWithHorizntalAppends(5, createLabel("Firmware Version: "),  firmware ,     NULL),
        createBoxWithHorizntalAppends(5, createLabel("Kernel Version: "),    kernel ,       NULL),
        createBoxWithHorizntalAppends(5, createLabel("Architecture: "),      architecture , NULL),
        createSeparatorWithHorizntal(),
        createBoxWithHorizntalAppends(5, createLabel("Runtime: "),           runtime ,      NULL),
        createBoxWithHorizntalAppends(5, createLabel("Systime: "),           systime ,      NULL),
        createBoxWithHorizntalAppends(5, createLabel("Load: "),              load ,         NULL),
        createSeparatorWithHorizntal(),
        timer_ctl_box,
        NULL
    );
    setBoxFullMargin(mainbox, 30);

    setWindowChild(window, mainbox);
    showWindow(window);
}

int	main(int argc, char **argv) {

    GtkApplication *app;
    int status;

    app = createApplicationConnectActivate("gtk.example.systeminfo", 
                                G_APPLICATION_FLAGS_NONE, activate);
    // app = gtk_application_new("gtk.example.template", G_APPLICATION_FLAGS_NONE);
    // g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    // status = g_application_run(G_APPLICATION(app), argc, argv);
    // g_object_unref(app);
    status = runApplication(app, argc, argv);
    objectUnref(app);

    return status;
}

